package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {

    @Reference
    private MemberService memberService;
    @Reference
    private SetMealService setMealService;
    @Reference
    private ReportService reportService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        Map<String,Object> map = memberService.getMemberReport();
        log.debug("ReportController: getMemberReport: map ===> " +map);
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }

    @RequestMapping("/getSetMealReport")
    public Result getSetMealReport(){
        List<Map<String,Object>> mapList = setMealService.findSetMealCount();
        log.debug("ReportController: getSetMealReport: mapList ===> " + mapList);
        List<String> setMealName = new ArrayList<String>();
        for (Map<String, Object> map : mapList) {
            String name = (String) map.get("name");
            setMealName.add(name);
        }
        log.debug("ReportController: getSetMealReport: setMealNameList ===> " + setMealName);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("setmealCount",mapList);
        map.put("setmealNames",setMealName);
        log.debug("ReportController: getSetMealReport: map ===> " + map);
        return new Result(true,MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);

    }

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        Map<String,Object> map = null;
        try {
            map = reportService.getBusinessReport();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_BUSINESS_REPORT_FAIL);
        }
        log.debug("ReportController: getBusinessReportData: map ===> " + map);
        return new Result(true,MessageConst.GET_BUSINESS_REPORT_SUCCESS,map);
    }

    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response) {
        try{
        Map<String,Object> map = reportService.getBusinessReport();
        Object reportDate = map.get("reportDate");
        Object todayNewMember = map.get("todayNewMember");
        Object totalMember = map.get("totalMember");
        Object thisWeekNewMember = map.get("thisWeekNewMember");
        Object thisMonthNewMember = map.get("thisMonthNewMember");
        Object todayOrderNumber = map.get("todayOrderNumber");
        Object todayVisitsNumber = map.get("todayVisitsNumber");
        Object thisWeekOrderNumber = map.get("thisWeekOrderNumber");
        Object thisWeekVisitsNumber = map.get("thisWeekVisitsNumber");
        Object thisMonthOrderNumber = map.get("thisMonthOrderNumber");
        Object thisMonthVisitsNumber = map.get("thisMonthVisitsNumber");
        List<Map<String,Object>> hotSetmeal = (List<Map<String,Object>>) map.get("hotSetmeal");

        InputStream inputStream = ReportController.class.getClassLoader().getResourceAsStream("report_template.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        XSSFSheet sheetAt = workbook.getSheetAt(0);
        XSSFRow row = sheetAt.getRow(2);
        XSSFCell cell = row.getCell(5);
        cell.setCellValue(String.valueOf(reportDate));

        row = sheetAt.getRow(4);
        cell = row.getCell(5);
        cell.setCellValue(String.valueOf(todayNewMember));
        cell = row.getCell(7);
        cell.setCellValue(String.valueOf(totalMember));

        row = sheetAt.getRow(5);
        cell = row.getCell(5);
        cell.setCellValue(String.valueOf(thisWeekNewMember));
        cell = row.getCell(7);
        cell.setCellValue(String.valueOf(thisMonthNewMember));

        row = sheetAt.getRow(7);
        cell = row.getCell(5);
        cell.setCellValue(String.valueOf(todayOrderNumber));
        cell = row.getCell(7);
        cell.setCellValue(String.valueOf(todayVisitsNumber));

        row = sheetAt.getRow(8);
        cell = row.getCell(5);
        cell.setCellValue(String.valueOf(thisWeekOrderNumber));
        cell = row.getCell(7);
        cell.setCellValue(String.valueOf(thisWeekVisitsNumber));

        row = sheetAt.getRow(9);
        cell = row.getCell(5);
        cell.setCellValue(String.valueOf(thisMonthOrderNumber));
        cell = row.getCell(7);
        cell.setCellValue(String.valueOf(thisMonthVisitsNumber));

        int rowNum = 12;
        for (Map<String, Object> setmeal : hotSetmeal) {
            row = sheetAt.getRow(rowNum);
            cell = row.getCell(4);
            cell.setCellValue(String.valueOf(setmeal.get("name")));

            cell = row.getCell(5);
            cell.setCellValue(String.valueOf(setmeal.get("setmeal_count")));

            cell = row.getCell(6);
            cell.setCellValue(String.valueOf(setmeal.get("proportion")));

            rowNum++;
        }
        ServletOutputStream out = response.getOutputStream();
        //指定附件的格式为excel
        response.setContentType("application/vnd.ms-excel");
        //attachment 以附件下载
        //下载弹出框架中的默认的文件名
        response.setHeader("content-Disposition","attachment;filename="+reportDate+"_report.xlsx");
        //输出流下载
        workbook.write(out);
        out.flush();
        out.close();
        workbook.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
